package com.coldworks.coldjoke.adapter;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.model.ReplyModel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ReplyAdapter extends BaseAdapter {

	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private Context context;
	private List<ReplyModel> list;

	public ReplyAdapter(Context context, List<ReplyModel> list) {
		this.context = context;
		this.list = list;
	}

	public void setList(List<ReplyModel> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ReplyViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(this.context).inflate(
					R.layout.reply_listview_item, null);
			holder = new ReplyViewHolder();
			holder.userNameText = (TextView) convertView
					.findViewById(R.id.reply_user_name);
			holder.replyContentText = (TextView) convertView
					.findViewById(R.id.reply_content);
			holder.floorText = (TextView) convertView
					.findViewById(R.id.reply_floor);
			convertView.setTag(holder);
		} else {
			holder = (ReplyViewHolder) convertView.getTag();
		}

		ReplyModel replyModel = list.get(position);

		if (replyModel != null) {
			holder.userNameText.setText(replyModel.getUserName());
			holder.replyContentText.setText(replyModel.getReplyContent());
			holder.floorText.setText(String.valueOf(position + 1) + "F");
		}

		return convertView;
	}

	static class ReplyViewHolder {
		TextView userNameText;
		TextView replyContentText;
		TextView floorText;
	}
}
